<template>
    <div class="sidebar">
        <el-menu
            class="sidebar-el-menu"
            :default-active="getActive"
            :collapse="collapse"
            background-color="#324157"
            text-color="#bfcbd9"
            active-text-color="#20a0ff"
            :unique-opened="true"
            router
        >
            <template v-for="item in app.route">
                <template v-if="item.subs && item.subs.length != 0">
                    <el-submenu :index="item.index" :key="item.index">
                        <!-- 一级菜单目录 -->
                        <template slot="title">
                            <i :class="item.icon"></i>
                            <span slot="title">{{ item.title }}</span>
                        </template>
                        <template v-for="subItem in item.subs">
                            <el-submenu v-if="subItem.subs" :index="subItem.index" :key="subItem.index">
                                <!-- 二级菜单目录 -->
                                <template slot="title">{{ subItem.title }}</template>
                                <!-- 三级菜单文件 -->
                                <el-menu-item
                                    v-for="(threeItem, i) in subItem.subs"
                                    :key="i"
                                    :index="getSplit(threeItem.index)"
                                    :route="{ path: threeItem.index }"
                                    >{{ threeItem.title }}</el-menu-item
                                >
                            </el-submenu>
                            <!-- 二级菜单文件 -->
                            <el-menu-item v-else :index="getSplit(subItem.index)" :route="{ path: subItem.index }" :key="subItem.index">{{
                                subItem.title
                            }}</el-menu-item>
                        </template>
                    </el-submenu>
                </template>
                <!-- 一级菜单文件 -->
                <template v-else>
                    <el-menu-item :index="getSplit(item.index)" :route="{ path: item.index }" :key="item.index">
                        <i :class="item.icon"></i>
                        <span slot="title">{{ item.title }}</span>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script>
import { mapState } from 'vuex';
import bus from '../common/bus';
export default {
    data() {
        return {
            collapse: false
        };
    },
    computed: {
        ...mapState(['app']),
        getActive() {
            var path = this.$route.path.split('/');
            // console.log(path);
            return path[1];
        }
    },
    methods: {
        getSplit(path) {
            if (path) {
                return path.split('/')[1];
            } else {
                return '/';
            }
        }
    },
    created() {
        // 通过 Event Bus 进行组件间通信，来折叠侧边栏
        bus.$on('collapse', msg => {
            this.collapse = msg;
            bus.$emit('collapse-content', msg);
        });
    }
};
</script>

<style scoped>
.el-menu--inline.el-menu > .el-menu-item {
    background-color: #1f2d3d !important;
}
.el-menu--inline.el-menu > .el-menu-item:hover {
    background-color: #001528 !important;
}
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
    width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
    width: 200px;
}
.sidebar > ul {
    height: 100%;
}
</style>
